/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 7/27/2010
 * Time: 3:03 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;

namespace popLib.ExtensionClass
{
	/// <summary>
	/// Description of INISetting.
	/// </summary>
	public class INISetting
	{
		Hashtable sections;
		string file;
		
		public INISetting(string file){
			sections = new Hashtable();
			this.Filename = file;
			Load();
		}
			
		public void Load(){
			if( !File.Exists(Filename) ){				
				string path = Path.GetDirectoryName(Filename);
				if( !Directory.Exists(path) ) Directory.CreateDirectory( path );
				FileLib.writeFile( Filename, "" );
			}
			
			PartIniContent( FileLib.readFile( Filename ) );	
		}
		
		private void PartIniContent( string content ){
			sections.Clear();			
			string[] lines = StringLib.splitByAny( content, new string[]{"\r\n"} );
			string currentSectionKey = null;
			foreach( string line in lines ){
				string lineContent = line.Trim();
				
				//section name here
				if( lineContent.StartsWith("[") && lineContent.EndsWith("]") ){
					currentSectionKey = lineContent.Substring( 1, lineContent.Length - 2);
					INISection section = new INISection( currentSectionKey );
					sections.Add( section.Name, section );					
				}
				//key value here
				else if( lineContent.Contains("=") && !lineContent.StartsWith("=") )
				{ 
					string key   = lineContent.Substring( 0, lineContent.IndexOf("=") );
					string value = lineContent.Substring( lineContent.IndexOf("=")+1 );				
					((INISection)sections[ currentSectionKey ]).AddOrUpdateValue( key, value );
				}
			}
		}
		
		public void Save(){
			string content = "";
			foreach( INISection section in sections.Values ){
				content += section.GetSectionText() + "\r\n\r\n";
			}			
			FileLib.writeFile( Filename, content, false, System.Text.Encoding.Unicode );
		}
		
		public void SetValue( string section, string key, string value ){
			if( sections.ContainsKey( section ) ){
				((INISection)sections[section]).AddOrUpdateValue( key, value );
			}else{
				INISection newsection = new INISection( section );
				newsection.AddOrUpdateValue( key, value );
				sections.Add( section, newsection );
				if( AutoSave ) Save();
			}
		}
		
		public string GetValue( string section, string key, string defaulValue ){
			if( sections.ContainsKey( section ) ){
				return ((INISection)sections[section]).GetValue( key, defaulValue );
			}else{
				INISection newsection = new INISection( section );
				newsection.AddOrUpdateValue( key, defaulValue );
				sections.Add( section, newsection );
				if( AutoSave ) Save();
				return defaulValue;
			}
		}
		
		public string[] GetKeyNames(string section){
			if( sections.ContainsKey( section ) ){
				return ((INISection)sections[section]).GetAllKeys();
			}else{
				return null;
			}
		}
		
		public string[] GetSectionNames(){
			string[] result = new string[sections.Count];
			sections.Keys.CopyTo( result, 0 );
			return result;
		}
		
		public INISection[] GetSections(){
			INISection[] result = new INISection[sections.Count];
			sections.Values.CopyTo( result, 0 );
			return result;			
		}
		
		public void RemoveSection( string section ){
			if( sections.ContainsKey( section ) ) {
				sections.Remove( section );
				if( AutoSave ) Save();
			}
		}
		
		public void RemoveKey( string section, string key ){
			if( sections.ContainsKey( section ) ) {
				((INISection)sections[section]).RemoveKey(key);
				if( AutoSave ) Save();
			}
		}
		
		bool autosave = false;
		public bool AutoSave{
			get{ return autosave; }
			set{ autosave = value; }
		}
		
		public string Filename{
			get{ return file; }
			set{
				file = value; 
				if( !Path.IsPathRooted(file) )
					file = FileLib.getFullPath(value);				
			}
		}
	}
	
	public class INISection{
		Hashtable items;
		public INISection(string name){
			this.Name = name;
			items = new Hashtable();
		}
		
		private string name;
		public string Name{
			get{ return name; }
			set{ name = value; }
		}
		
		public string[] GetAllKeys(){
			string[] result = new string[items.Count];
			items.Keys.CopyTo( result, 0 );
			return result;			
		}
		
		public void AddOrUpdateValue( string key, string value ){
			if( items.ContainsKey(key) ){
				items[key] = value;
			}else items.Add( key, value );
		}
		
		public string GetValue( string key, string defaultValue ){
			if( items.ContainsKey(key) ){
				return items[key].ToString();
			}else{
				items.Add( key, defaultValue );
				return defaultValue;
			}
		}
		
		public void RemoveKey(string key){
			if( items.ContainsKey( key ) ){
				items.Remove( key );
			}
		}
					
		public string GetSectionText(){
			string content = "["+Name+"]\r\n";
			foreach( string key in items.Keys ){
				content += string.Format("{0}={1}\r\n", key, items[key] );
			}
			return content;
		}
	}
}
